@echo off
setlocal
set MSTEST="%VS90COMNTOOLS%\..\ide\mstest.exe"
set RAR="%ProgramFiles%\WinRar\rar.exe"
set SED="..\..\..\..\tools\sed\sed.exe"

title Testing Scrum Sprint Monitor and creating release archive (started at %DATE% %TIME%)

echo Creating screensaver release archive at %DATE% %TIME%...

pushd build\x86\Release

if errorlevel 1 goto ErrorFound

@rem Clear staging folder
echo Deleting staging folder...

del staging /Q

@rem Copy output to the staging folder
echo Copying output to staging folder...

xcopy . .\staging /Y /I
xcopy .\AddIns .\staging\AddIns /Y /I /S

pushd staging

echo Running the tests

@rem Run the tests
del testResults.trx /Q

echo Executing unit tests...
%MSTEST% /testcontainer:ScrumSprintMonitor.Impl.Test.dll /testcontainer:ScrumSprintMonitor.BuildMonitor.Test.dll /testcontainer:ScrumSprintMonitor.WorkItemMonitor.Test.dll /runconfig:..\..\..\..\LocalTestRun.testrunconfig /resultsfile:testResults.trx

rem if errorlevel 1 goto TestErrorFound

@rem Delete debug files
del *.pdb /Q
del *.xml /Q
del AddIns\*.pdb /Q
del AddIns\*.xml /Q
del AddIns\ServerAdapters\*.pdb /Q
del AddIns\ServerAdapters\*.xml /Q

@rem Do not include any TFS DLLs
del Microsoft.TeamFoundation*.* /Q

@rem Copy configuration files to staging folder
echo Copying configuration files to staging folder...
xcopy ..\..\..\..\docs\config . /Y /I

@rem Make screensaver
rename ScrumSprintMonitor.Wpf.Application.exe.* ScrumSprintMonitor.Wpf.Application.scr.*

@rem Rename exe to scr inside configuration files
%SED% -c -i s/.exe/.scr/g ScrumSprintMonitor.Wpf.Application.application
%SED% -c -i s/.exe/.scr/g ScrumSprintMonitor.Wpf.Application.scr.config
%SED% -c -i s/.exe/.scr/g ScrumSprintMonitor.Wpf.Application.scr.manifest

@rem Create archive file
echo Creating archive file...
%RAR% a -df -m5 -s -t -tl ScrumSprintMonitor.rar . .\AddIns

popd
popd
echo Finished creating screensaver at %DATE% %TIME%.
@rem Open Windows Explorer on staging folder
start build\x86\Release\staging
@goto Exit

:Exit
echo on
@exit

:ErrorFound
echo Error found!
goto ErrorExit

:TestErrorFound
echo Tests failed!
goto ErrorExit

:ErrorExit
echo on